/**
 * This class represents a productgroup-handler
 * 
 * @author Konrad Paumann
 * @author Bernhard Denner
 * @author Maximilian Csuk
 * 
 * $Revision$ 
 * $Date$
 * 
 */

public class ProductGroupHandler extends Handler {
	/**
	 * variables
	 */
	private final ProductGroup productGroup;
	
	/**
	 * constructor
	 * @param g the product-group to handle
	 */
	public ProductGroupHandler(ProductGroup productGroup, User user, Logger logger) {
		super(user, logger);
		assert productGroup != null;
		this.productGroup = productGroup;
	}
	
	/**
	 * get the tax
	 * @return the tax
	 */
	public int getTax() throws UserException {
		if (!getUser().canReadProductGroup()) {
			getLogger().log(getUser().getName(), "Lesen von Produkt-Gruppen-Mwst verweigert");
			throw new UserException("User nicht authorisiert");
		}
		
		return productGroup.getTax();
	}
	
	/**
	 * set the tax
	 * @param the tax
	 */
	public void setTax(int tax) throws UserException {
		if (!getUser().canWriteProductGroup()) {
			getLogger().log(getUser().getName(), "Aendern von Produkt-Gruppen-Mwst verweigert");
			throw new UserException("User nicht authorisiert");
		}

		productGroup.setTax(tax);
		
		getLogger().log(getUser().getName(), "Aendern von Produkt-Gruppen-Mwst erfolgreich");
	}
	
	/**
	 * get the name
	 * @return the name
	 */
	public String getName() throws UserException {
		if (!getUser().canReadProductGroup()) {
			getLogger().log(getUser().getName(), "Lesen von Produkt-Gruppen-Name verweigert");
			throw new UserException("User nicht authorisiert");
		}

		return productGroup.getGroupName();
	}
	
	/**
	 * get the display-string
	 * @return the display-string
	 */
	public String getDisplayString() throws UserException {
		if (!getUser().canReadProductGroup()) {
			getLogger().log(getUser().getName(), "Lesen von Produkt-Gruppen-Ausgabe verweigert");
			throw new UserException("User nicht authorisiert");
		}

		return productGroup.getDisplayString();
	}
	
	/**
	 * adds a product to the group
	 * @param shortDescription
	 * @param description
	 * @param price
	 * @param amount
	 * @throws UserException
	 * @throws ProductException
	 */
	public void addProduct(String shortDescription, String description, int price,
			int amount) throws UserException, ProductException {
		if (!getUser().canWriteProductGroup()) {
			getLogger().log(getUser().getName(), "Hinzufuegen von Produkt verweigert");
			throw new UserException("User nicht authorisiert");
		}

		try {
			productGroup.addProduct(new Product(shortDescription, description, price, amount));
		} catch (ProductException e) {
			throw new ProductException(e.getMessage());
		}
		
		getLogger().log(getUser().getName(), "Hinzufuegen von Produkt erfolgreich");
	}
	
	/**
	 * removes a product from the group
	 * @param shortDescription
	 * @param description
	 * @param price
	 * @param amount
	 * @throws UserException
	 * @throws ProductException
	 */
	public void removeProduct(String shortDescription) throws UserException, ProductException {
		if (!getUser().canWriteProductGroup()) {
			getLogger().log(getUser().getName(), "Entfernen von Produkt verweigert");
			throw new UserException("User nicht authorisiert");
		}

		try {
			productGroup.removeProduct(shortDescription);
		} catch (ProductException e) {
			throw new ProductException(e.getMessage());
		}
		
		getLogger().log(getUser().getName(), "Entfernen von Produkt erfolgreich");
	}
}
